// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso;

import org.joml.Vector2f;
import zombie.iso.SpriteDetails.IsoFlagType;
import zombie.popman.ObjectPool;
import zombie.util.list.PZArrayList;

public final class IsoWaterGeometry {
    private static final Vector2f tempVector2f = new Vector2f();
    boolean hasWater = false;
    boolean bShore = false;
    final float[] x = new float[4];
    final float[] y = new float[4];
    final float[] depth = new float[4];
    final float[] flow = new float[4];
    final float[] speed = new float[4];
    float IsExternal = 0.0F;
    IsoGridSquare square = null;
    int m_adjacentChunkLoadedCounter;
    public static final ObjectPool<IsoWaterGeometry> pool = new ObjectPool<>(IsoWaterGeometry::new);

    public IsoWaterGeometry init(IsoGridSquare _square) throws Exception {
        this.x[0] = IsoUtils.XToScreen((float)_square.x, (float)_square.y, 0.0F, 0);
        this.y[0] = IsoUtils.YToScreen((float)_square.x, (float)_square.y, 0.0F, 0);
        this.x[1] = IsoUtils.XToScreen((float)_square.x, (float)(_square.y + 1), 0.0F, 0);
        this.y[1] = IsoUtils.YToScreen((float)_square.x, (float)(_square.y + 1), 0.0F, 0);
        this.x[2] = IsoUtils.XToScreen((float)(_square.x + 1), (float)(_square.y + 1), 0.0F, 0);
        this.y[2] = IsoUtils.YToScreen((float)(_square.x + 1), (float)(_square.y + 1), 0.0F, 0);
        this.x[3] = IsoUtils.XToScreen((float)(_square.x + 1), (float)_square.y, 0.0F, 0);
        this.y[3] = IsoUtils.YToScreen((float)(_square.x + 1), (float)_square.y, 0.0F, 0);
        this.hasWater = false;
        this.bShore = false;
        this.square = _square;
        this.IsExternal = _square.getProperties().Is(IsoFlagType.exterior) ? 1.0F : 0.0F;
        int int0 = IsoWaterFlow.getShore(_square.x, _square.y);
        IsoObject isoObject = _square.getFloor();
        String string = isoObject == null ? null : isoObject.getSprite().getName();
        if (_square.getProperties().Is(IsoFlagType.water)) {
            this.hasWater = true;

            for (int int1 = 0; int1 < 4; int1++) {
                this.depth[int1] = 1.0F;
            }
        } else if (int0 == 1 && string != null && string.startsWith("blends_natural")) {
            for (int int2 = 0; int2 < 4; int2++) {
                this.depth[int2] = 0.0F;
            }

            IsoGridSquare isoGridSquare0 = _square.getAdjacentSquare(IsoDirections.W);
            IsoGridSquare isoGridSquare1 = _square.getAdjacentSquare(IsoDirections.NW);
            IsoGridSquare isoGridSquare2 = _square.getAdjacentSquare(IsoDirections.N);
            IsoGridSquare isoGridSquare3 = _square.getAdjacentSquare(IsoDirections.SW);
            IsoGridSquare isoGridSquare4 = _square.getAdjacentSquare(IsoDirections.S);
            IsoGridSquare isoGridSquare5 = _square.getAdjacentSquare(IsoDirections.SE);
            IsoGridSquare isoGridSquare6 = _square.getAdjacentSquare(IsoDirections.E);
            IsoGridSquare isoGridSquare7 = _square.getAdjacentSquare(IsoDirections.NE);
            if (isoGridSquare2 == null
                || isoGridSquare1 == null
                || isoGridSquare0 == null
                || isoGridSquare3 == null
                || isoGridSquare4 == null
                || isoGridSquare5 == null
                || isoGridSquare6 == null
                || isoGridSquare7 == null) {
                return null;
            }

            if (isoGridSquare0.getProperties().Is(IsoFlagType.water)
                || isoGridSquare1.getProperties().Is(IsoFlagType.water)
                || isoGridSquare2.getProperties().Is(IsoFlagType.water)) {
                this.bShore = true;
                this.depth[0] = 1.0F;
            }

            if (isoGridSquare0.getProperties().Is(IsoFlagType.water)
                || isoGridSquare3.getProperties().Is(IsoFlagType.water)
                || isoGridSquare4.getProperties().Is(IsoFlagType.water)) {
                this.bShore = true;
                this.depth[1] = 1.0F;
            }

            if (isoGridSquare4.getProperties().Is(IsoFlagType.water)
                || isoGridSquare5.getProperties().Is(IsoFlagType.water)
                || isoGridSquare6.getProperties().Is(IsoFlagType.water)) {
                this.bShore = true;
                this.depth[2] = 1.0F;
            }

            if (isoGridSquare6.getProperties().Is(IsoFlagType.water)
                || isoGridSquare7.getProperties().Is(IsoFlagType.water)
                || isoGridSquare2.getProperties().Is(IsoFlagType.water)) {
                this.bShore = true;
                this.depth[3] = 1.0F;
            }
        }

        Vector2f vector2f = IsoWaterFlow.getFlow(_square, 0, 0, tempVector2f);
        this.flow[0] = vector2f.x;
        this.speed[0] = vector2f.y;
        vector2f = IsoWaterFlow.getFlow(_square, 0, 1, vector2f);
        this.flow[1] = vector2f.x;
        this.speed[1] = vector2f.y;
        vector2f = IsoWaterFlow.getFlow(_square, 1, 1, vector2f);
        this.flow[2] = vector2f.x;
        this.speed[2] = vector2f.y;
        vector2f = IsoWaterFlow.getFlow(_square, 1, 0, vector2f);
        this.flow[3] = vector2f.x;
        this.speed[3] = vector2f.y;
        this.hideWaterObjects(_square);
        return this;
    }

    private void hideWaterObjects(IsoGridSquare isoGridSquare) {
        PZArrayList pZArrayList = isoGridSquare.getObjects();

        for (int _int = 0; _int < pZArrayList.size(); _int++) {
            IsoObject isoObject = (IsoObject)pZArrayList.get(_int);
            if (isoObject.sprite != null && isoObject.sprite.name != null) {
                String string = isoObject.sprite.name;
                if (string.startsWith("blends_natural_02")
                    && (
                        string.endsWith("_0")
                            || string.endsWith("_1")
                            || string.endsWith("_2")
                            || string.endsWith("_3")
                            || string.endsWith("_4")
                            || string.endsWith("_5")
                            || string.endsWith("_6")
                            || string.endsWith("_7")
                            || string.endsWith("_8")
                            || string.endsWith("_9")
                            || string.endsWith("_10")
                            || string.endsWith("_11")
                            || string.endsWith("_12")
                    )) {
                    isoObject.sprite.setHideForWaterRender();
                }
            }
        }
    }

    public boolean isShore() {
        return IsoWaterFlow.getShore(this.square.x, this.square.y) == 0;
    }

    public float getFlow() {
        IsoWaterFlow.getShore(this.square.x, this.square.y);
        Vector2f vector2f = IsoWaterFlow.getFlow(this.square, 0, 0, tempVector2f);
        System.out.println("FLOW!  " + vector2f.x + " " + vector2f.y);
        return vector2f.x;
    }
}
